package com.braveman.im.ui.weiget;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.ui.activity.SplashActivity;
import com.braveman.im.ui.activity.message.AddFriendsAndGroupActivity;
import com.braveman.im.ui.activity.message.QrCodeActivity;
import com.braveman.im.ui.activity.message.SelectCreateGroupStatesActivity;
import com.braveman.im.ui.activity.message.SendAdvertisingActivity;
import com.braveman.im.ui.activity.message.SendLoveActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;

import razerdp.basepopup.BasePopupWindow;

/**
 * Created by 大灯泡 on 2016/1/22.
 * 菜单。
 */
public class MenuPopup extends BasePopupWindow implements View.OnClickListener {

    private Activity context;


    public MenuPopup(Activity context) {
        super(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        findViewById(R.id.tx_1).setOnClickListener(this);
        findViewById(R.id.tx_2).setOnClickListener(this);
        findViewById(R.id.tx_3).setOnClickListener(this);
        findViewById(R.id.tx_4).setOnClickListener(this);
        findViewById(R.id.tx_5).setOnClickListener(this);
        findViewById(R.id.tx_6).setOnClickListener(this);
        this.context = context;
    }

    @Override
    protected Animation initShowAnimation() {
//        AnimationSet set=new AnimationSet(false);
//        Animation shakeAnima=new RotateAnimation(0,15,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        shakeAnima.setInterpolator(new CycleInterpolator(5));
//        shakeAnima.setDuration(400);
//        set.addAnimation(getDefaultAlphaAnimation());
//        set.addAnimation(shakeAnima);

        AnimationSet set = new AnimationSet(true);
        set.setInterpolator(new DecelerateInterpolator());
        set.addAnimation(getScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0));
        set.addAnimation(getDefaultAlphaAnimation());


        return set;
//        return null;
    }

    @Override
    public Animator initShowAnimator() {
       /* AnimatorSet set=new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(mAnimaView,"scaleX",0.0f,1.0f).setDuration(300),
                ObjectAnimator.ofFloat(mAnimaView,"scaleY",0.0f,1.0f).setDuration(300),
                ObjectAnimator.ofFloat(mAnimaView,"alpha",0.0f,1.0f).setDuration(300*3/2));*/
        return null;
    }

    @Override
    public void showPopupWindow(View v) {
        setOffsetX(-(getWidth() - v.getWidth() / 2));
        setOffsetY(-v.getHeight() / 2);
        super.showPopupWindow(v);
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.popup_menu);
    }

    @Override
    public View initAnimaView() {
        return getPopupWindowView().findViewById(R.id.popup_contianer);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tx_1:
                ActivityUtils.startActivity(context, SelectCreateGroupStatesActivity.class);
                dismiss();
                break;
            case R.id.tx_2:
                ActivityUtils.startActivity(context, AddFriendsAndGroupActivity.class);
                dismiss();
                break;
            case R.id.tx_3:
                startScanQrcode();
                dismiss();
                break;
            case R.id.tx_4:
                UIUtils.showToast("敬请期待");
                dismiss();
                break;
            case R.id.tx_5:
                ActivityUtils.startActivity(context, SendLoveActivity.class);
                dismiss();
                break;
            case R.id.tx_6:
                ActivityUtils.startActivity(context, SendAdvertisingActivity.class);
                dismiss();
                break;
            default:
                break;

        }

    }

    public void startScanQrcode() {
        //                假如你要用的是fragment进行界面的跳转
//                IntentIntegrator intentIntegrator = IntentIntegrator.forSupportFragment(ShopFragment.this)
//
        IntentIntegrator intentIntegrator = new IntentIntegrator(context);
        intentIntegrator
                .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                .setPrompt("")
                .setOrientationLocked(false)//扫描方向固定
                .setCaptureActivity(QrCodeActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }

}
